Imports BlogML.Xml
Imports DotNetNuke.Modules.Blog.Business
Imports System.Linq
Imports System.Xml
Imports System.IO
Imports DotNetNuke.Modules.Blog.Settings
Imports DotNetNuke.Services.Localization


Public Class GraffitiReader
  Inherits BlogReader

  Protected _tagCollection As New Dictionary(Of String, String)()

  Public Overrides Function Import() As Boolean
    ValidateProperties()

    'Initialize BlogML
    Dim blog As BlogMLBlog

    Try
      blog = BlogMLSerializer.Deserialize(XmlReader)
    Catch ex As Exception
      'Handle failure to import xml

    End Try

    Dim BlogModuleSettings As BlogSettings = BlogSettings.GetBlogSettings(PortalID, TabID)
    _categoryMapping.UseChildBlogs = BlogModuleSettings.AllowChildBlogs

    If blog IsNot Nothing Then
      If blog.Categories.Count > 0 Then
        LoadCategories(blog.Categories, BlogID)
      End If
      If blog.Posts.Count > 0 Then
        LoadTags()
        LoadPosts(blog.Posts, BlogID)
      End If
    End If
    Utility.CreateAllEntryLinks(PortalID)
  End Function

  Protected Overrides Sub LoadPosts(ByVal MLPosts As BlogMLBlog.PostCollection, ByVal RootBlogID As Integer)
    For Each objPostML As BlogMLPost In MLPosts
      Dim newBlogEntry As EntryInfo = GetNewBlogEntry(objPostML)

      If _categoryMapping.UseChildBlogs AndAlso objPostML.Categories.Count > 0 Then
        newBlogEntry.BlogID = _categoryMapping.GetCategoryMapByCategoryRef(objPostML.Categories(0).Ref).ChildBlogID
      Else
        newBlogEntry.BlogID = RootBlogID
      End If

      newBlogEntry.Published = objPostML.Approved
      newBlogEntry.AddedDate = objPostML.DateCreated
      newBlogEntry.EntryID = _entryController.AddEntry(newBlogEntry)

      AssociateCategories(objPostML, newBlogEntry)

      OnLogMessage(New LogMessageEventArgs(GetFormattedMessage("CreatedPost", newBlogEntry.Title), LogMessageType.OkMessage))

      If _tagCollection.ContainsKey(objPostML.ID) Then
        TagController.UpdateTagsByEntry(newBlogEntry.EntryID, _tagCollection(objPostML.ID))
      End If

      If objPostML.Comments.Count > 0 Then
        ImportComments(newBlogEntry.EntryID, objPostML.Comments)
      End If

      If objPostML.Trackbacks.Count > 0 Then
        ImportTrackbacks(newBlogEntry.EntryID, objPostML.Trackbacks)
      End If
    Next

  End Sub

  Protected Overrides Sub OnLogMessage(ByVal e As LogMessageEventArgs)
    MyBase.OnLogMessage(e)
  End Sub

  Protected Sub LoadTags()
    _tagCollection.Clear()
    Dim doc As New XmlDocument()
    doc.Load(Me.XmlReader)
    Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
    nsmgr.AddNamespace("bml", "http://www.blogml.com/2006/09/BlogML")
    Dim posts As XmlNodeList = doc.GetElementsByTagName("post")
    For Each post As XmlNode In posts
      Dim tags As New ArrayList()
      If post.ChildNodes.Count <= 0 Then
        Continue For
      End If
      Dim postid As String = post.Attributes("id").Value
      Dim tagNodes As XmlNodeList = post.SelectNodes("descendant::bml:tags/bml:tag", nsmgr)
      For Each tagNode As XmlNode In tagNodes
        If tagNode.Attributes IsNot Nothing Then
          tags.Add(tagNode.Attributes("ref").Value)
        End If
      Next
      If tags.Count > 0 Then
        _tagCollection.Add(postid, String.Join(",", tags.ToArray("".GetType())))
      End If
    Next
  End Sub
End Class
